package com.cc.controller;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@RestController
@CrossOrigin
@RequestMapping("es")
public class EsController {

    @RequestMapping("/search1")
    public List<Map<String, Object>> search1(@RequestParam("name") String name) throws IOException {
        // 创建Java和ES的链接
        HttpHost httpHost = new HttpHost("127.0.0.1", 9200, "http");
        RestClientBuilder builder = RestClient.builder(httpHost);
        RestHighLevelClient client = new RestHighLevelClient(builder);
        // 编写查询操作
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("goods");
        // 构建查询条件
        SearchSourceBuilder query = new SearchSourceBuilder()
                .query(QueryBuilders.matchQuery("name", name))
                .size(20); // 设置返回结果的数量为20
        // 设置高亮显示
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.preTags("<font color='red'>");
        highlightBuilder.postTags("</font>");
        highlightBuilder.field("name");
        query.highlighter(highlightBuilder);
        // 建立查询条件的关系
        searchRequest.source(query);
        // 发送请求
        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
        // 展示响应结果
        SearchHits hits = response.getHits();
        List<Map<String, Object>> results = new ArrayList<>();
        for (SearchHit hit : hits) {
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            // 添加高亮结果
            Map<String, HighlightField> highlightFields = hit.getHighlightFields();
            if (highlightFields.containsKey("name")) {
                HighlightField highlightField = highlightFields.get("name");
                sourceAsMap.put("highlightedName", highlightField.fragments()[0].toString());
            }
            results.add(sourceAsMap);
        }
        // 关闭连接
        client.close();
        return results;
    }



    // goods销量排序，根据外键查询
    @RequestMapping("/sortGoodssales")
    public List<Map<String, Object>> sortGoodssales(@RequestParam("id") String id) throws IOException {
        // 创建Java和ES的链接
        HttpHost httpHost = new HttpHost("127.0.0.1", 9200, "http");
        RestClientBuilder builder = RestClient.builder(httpHost);
        RestHighLevelClient client = new RestHighLevelClient(builder);
        // 编写查询操作
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("goods");
        // 构建查询条件
        SearchSourceBuilder query = new SearchSourceBuilder();
        query.query(QueryBuilders.termQuery("uid", id));
        query.sort(SortBuilders.fieldSort("sales").order(SortOrder.DESC));
        query.size(20);
        // 建立查询条件的关系
        searchRequest.source(query);
        // 发送请求
        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
        // 展示响应结果
        SearchHits hits = response.getHits();
        List<Map<String, Object>> results = new ArrayList<>();
        for (SearchHit hit : hits) {
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            results.add(sourceAsMap);
        }
        // 关闭连接
        client.close();
        return results;
    }


    //goods销量排序
    @RequestMapping("/sortGoods")
    public List<Map<String, Object>> sortGoods() throws IOException {
        // 创建Java和ES的链接
        HttpHost httpHost = new HttpHost("127.0.0.1", 9200, "http");
        RestClientBuilder builder = RestClient.builder(httpHost);
        RestHighLevelClient client = new RestHighLevelClient(builder);
        // 编写查询操作
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("goods");
        // 构建查询条件
        SearchSourceBuilder query = new SearchSourceBuilder();
        // 添加排序条件
        query.sort(SortBuilders.fieldSort("sales").order(SortOrder.DESC));
        query.size(20);
        // 建立查询条件的关系
        searchRequest.source(query);
        // 发送请求
        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
        // 展示响应结果
        SearchHits hits = response.getHits();
        List<Map<String, Object>> results = new ArrayList<>();
        for (SearchHit hit : hits) {
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            results.add(sourceAsMap);
        }
        // 关闭连接
        client.close();
        return results;
    }



    //goods上新时间排序，根据外键查询
    @RequestMapping("/sortGoodsuptime")
    public List<Map<String, Object>> sortGoodsuptime(@RequestParam("id") String id) throws IOException {
        // 创建Java和ES的链接
        HttpHost httpHost = new HttpHost("127.0.0.1", 9200, "http");
        RestClientBuilder builder = RestClient.builder(httpHost);
        RestHighLevelClient client = new RestHighLevelClient(builder);
        // 编写查询操作
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("goods");
        // 构建查询条件
        SearchSourceBuilder query = new SearchSourceBuilder();
        query.query(QueryBuilders.termQuery("uid", id)); // 使用外键进行查询
        query.sort(SortBuilders.fieldSort("uptime").order(SortOrder.DESC));
        query.size(20);
        // 建立查询条件的关系
        searchRequest.source(query);
        // 发送请求
        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
        // 展示响应结果
        SearchHits hits = response.getHits();
        List<Map<String, Object>> results = new ArrayList<>();
        for (SearchHit hit : hits) {
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            results.add(sourceAsMap);
        }
        // 关闭连接
        client.close();

        return results;
    }


    //goods上新时间排序
    @RequestMapping("/sortuptime")
    public List<Map<String, Object>> sortuptime() throws IOException {
        // 创建Java和ES的链接
        HttpHost httpHost = new HttpHost("127.0.0.1", 9200, "http");
        RestClientBuilder builder = RestClient.builder(httpHost);
        RestHighLevelClient client = new RestHighLevelClient(builder);
        // 编写查询操作
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("goods");
        // 构建查询条件
        SearchSourceBuilder query = new SearchSourceBuilder();
        // 添加排序条件
        query.sort(SortBuilders.fieldSort("uptime").order(SortOrder.DESC));
        query.size(20);
        // 建立查询条件的关系
        searchRequest.source(query);
        // 发送请求
        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
        // 展示响应结果
        SearchHits hits = response.getHits();
        List<Map<String, Object>> results = new ArrayList<>();
        for (SearchHit hit : hits) {
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            results.add(sourceAsMap);
        }
        // 关闭连接
        client.close();
        return results;
    }

    // goods根据价格排序，并支持根据外键查询
    @RequestMapping("/sortGoodsprice")
    public List<Map<String, Object>> sortGoodsprice(@RequestParam(value = "a", required = false) String a,
                                                    @RequestParam(value = "id", required = false) String id) throws IOException {
        // 创建Java和ES的链接
        HttpHost httpHost = new HttpHost("127.0.0.1", 9200, "http");
        RestClientBuilder builder = RestClient.builder(httpHost);
        RestHighLevelClient client = new RestHighLevelClient(builder);
        // 编写查询操作
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("goods");
        // 构建查询条件
        SearchSourceBuilder query = new SearchSourceBuilder();
        // 如果有外键参数，则添加外键查询条件
        if (id != null && !id.isEmpty()) {
            query.query(QueryBuilders.termQuery("uid", id));
        }
        // 添加排序条件
        if ("1".equals(a)) {
            query.sort(SortBuilders.fieldSort("score2").order(SortOrder.ASC));
        } else {
            query.sort(SortBuilders.fieldSort("score2").order(SortOrder.DESC));
        }
        query.size(20);
        // 建立查询条件的关系
        searchRequest.source(query);
        // 发送请求
        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
        // 展示响应结果
        SearchHits hits = response.getHits();
        List<Map<String, Object>> results = new ArrayList<>();
        for (SearchHit hit : hits) {
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            results.add(sourceAsMap);
        }
        // 关闭连接
        client.close();
        return results;
    }


    // goods根据价格排序
    @RequestMapping("/sortprice")
    public List<Map<String, Object>> sortprice(@RequestParam("a") String a) throws IOException {
        // 创建Java和ES的链接
        HttpHost httpHost = new HttpHost("127.0.0.1", 9200, "http");
        RestClientBuilder builder = RestClient.builder(httpHost);
        RestHighLevelClient client = new RestHighLevelClient(builder);
        // 编写查询操作
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("goods");
        // 构建查询条件
        SearchSourceBuilder query = new SearchSourceBuilder();
        // 添加排序条件
        if ("1".equals(a)) {
            query.sort(SortBuilders.fieldSort("score2").order(SortOrder.ASC));
        } else {
            query.sort(SortBuilders.fieldSort("score2").order(SortOrder.DESC));
        }
        // 建立查询条件的关系
        searchRequest.source(query);
        query.size(20);
        // 发送请求
        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
        // 展示响应结果
        SearchHits hits = response.getHits();
        List<Map<String, Object>> results = new ArrayList<>();
        for (SearchHit hit : hits) {
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            results.add(sourceAsMap);
        }
        // 关闭连接
        client.close();
        return results;
    }









}
